﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ProjScan.Config;
using UnityEditor;
using UnityEngine;

namespace ProjScan.Tools
{
	// Token: 0x0200015F RID: 351
	internal class AssetPathManager
	{
		// Token: 0x06000C98 RID: 3224 RVA: 0x0003721C File Offset: 0x0003541C
		public int GetID(string assetPath)
		{
			assetPath = ToolsHelper.DirConvert(assetPath);
			int num = 0;
			bool flag = this.dicDir.TryGetValue(assetPath, out num);
			int result;
			if (flag)
			{
				result = num;
			}
			else
			{
				ScanLog.Instance.LogInfo(ScanLog.LogLevel.EXCEPTION, "AssetPathManager.GetDirId", "Cannot get dir id: " + assetPath);
				ScanLog.UnityLogError("Cannot find dir id");
				result = -1;
			}
			return result;
		}

		// Token: 0x06000C99 RID: 3225 RVA: 0x00037278 File Offset: 0x00035478
		public string GetAssetPath(int id)
		{
			foreach (KeyValuePair<string, int> keyValuePair in this.dicDir)
			{
				bool flag = keyValuePair.Value == id;
				if (flag)
				{
					return keyValuePair.Key;
				}
			}
			return string.Empty;
		}

		// Token: 0x06000C9A RID: 3226 RVA: 0x000372EC File Offset: 0x000354EC
		public void Init()
		{
			this.DMKLF6jLbm = Array.Empty<string>();
			this.NnTLINGV30 = Array.Empty<string>();
			this.t1KLZ6jKSt = Array.Empty<string>();
			this.zhxLVshQSh = Array.Empty<string>();
			this.assetPathList = new List<string>();
			this.scriptAssetsList = new HashSet<string>();
			this.assetPathHashSet = new HashSet<string>();
			this.artPathList = null;
			this.prefabPathList = null;
			bool flag = this.dicDir == null;
			if (flag)
			{
				this.dicDir = new Dictionary<string, int>();
			}
			else
			{
				this.dicDir.Clear();
			}
			this.globalConfig = (Singleton<ConfigMgr>.Instance.GetConfig<GlobalConfig>() as GlobalConfig);
			this.isInit = true;
			bool flag2 = !File.Exists(ToolsHelper.GetScanOutPut() + "/dirmap.csv");
			if (flag2)
			{
				FileStream fileStream = File.Create(ToolsHelper.GetScanOutPut() + "/dirmap.csv");
				fileStream.Close();
				fileStream.Dispose();
			}
		}

		// Token: 0x06000C9B RID: 3227 RVA: 0x000373DC File Offset: 0x000355DC
		public void FindAssetPath(bool isCheckDir)
		{
			bool flag = !this.isInit;
			if (flag)
			{
				throw new Exception("AssetPathManager has not been inited");
			}
			ToolsHelper.ShowProgressBar("Manager", "Collecting path info...", 0.01f, false);
			string[] array = Directory.GetDirectories(Application.dataPath, "*", SearchOption.AllDirectories);
			List<string> list = new List<string>();
			list.Add(ToolsHelper.ConvertAbsolutePathToRelative(ToolsHelper.DirConvert(Application.dataPath)));
			list.AddRange(array);
			array = list.ToArray();
			for (int i = 0; i < array.Length; i++)
			{
				string key = ToolsHelper.ConvertAbsolutePathToRelative(ToolsHelper.DirConvert(array[i]));
				this.dicDir.Add(key, i);
			}
			ToolsHelper.ShowProgressBar("Manager", "Collecting path info...", 0.2f, false);
			foreach (string text in Directory.GetFiles(Application.dataPath, "*", SearchOption.AllDirectories))
			{
				bool flag2 = !text.EndsWith(".meta", StringComparison.OrdinalIgnoreCase);
				if (flag2)
				{
					string text2 = ToolsHelper.ConvertAbsolutePathToRelative(ToolsHelper.DirConvert(text));
					bool flag3 = this.CheckFile(text2);
					if (flag3)
					{
						this.assetPathList.Add(text2);
					}
				}
			}
			ToolsHelper.ShowProgressBar("Manager", "Collecting path info...", 0.4f, false);
			List<string> list2 = new List<string>();
			this.scriptAssetsList = new HashSet<string>();
			List<string> list3 = new List<string>();
			for (int k = 0; k < this.assetPathList.Count; k++)
			{
				string text3 = this.assetPathList[k];
				string a = Path.GetExtension(text3).ToLower().Replace(".", "");
				bool flag4 = string.Equals(a, "prefab", StringComparison.Ordinal);
				if (flag4)
				{
					list2.Add(text3);
				}
				else
				{
					bool flag5 = string.Equals(a, "cs", StringComparison.Ordinal);
					if (flag5)
					{
						this.scriptAssetsList.Add(text3);
					}
					else
					{
						list3.Add(text3);
					}
				}
			}
			this.prefabPathList = list2.ToArray();
			this.artPathList = list3.ToArray();
			ToolsHelper.ShowProgressBar("Manager", "Collecting path info...", 0.6f, false);
			bool flag6 = this.globalConfig.globalAssetsScanMode == GlobalConfig.e_Global_Assets_Scan_Mode.All || this.globalConfig.globalTargetConfigs == null || this.globalConfig.globalTargetConfigs.Count == 0;
			if (flag6)
			{
				this.assetPathHashSet.UnionWith(this.assetPathList);
			}
			else
			{
				foreach (string text4 in this.assetPathList)
				{
					bool flag7 = false;
					bool flag8 = this.globalConfig.globalTargetConfigs != null;
					if (flag8)
					{
						foreach (ScanAssetsConfig scanAssetsConfig in this.globalConfig.globalTargetConfigs)
						{
							bool flag9 = (scanAssetsConfig.matchingType == MatchingType.regex && ToolsHelper.IsMatch(scanAssetsConfig.assetPath, text4)) || (scanAssetsConfig.matchingType == MatchingType.directory && ToolsHelper.IsDirContain(scanAssetsConfig.assetPath, text4));
							if (flag9)
							{
								flag7 = true;
								break;
							}
						}
					}
					bool flag10 = this.globalConfig.globalIgnoreConfigs != null;
					if (flag10)
					{
						foreach (ScanAssetsConfig scanAssetsConfig2 in this.globalConfig.globalIgnoreConfigs)
						{
							bool flag11 = (scanAssetsConfig2.matchingType == MatchingType.regex && ToolsHelper.IsMatch(scanAssetsConfig2.assetPath, text4)) || (scanAssetsConfig2.matchingType == MatchingType.directory && ToolsHelper.IsDirContain(scanAssetsConfig2.assetPath, text4));
							if (flag11)
							{
								flag7 = false;
								break;
							}
						}
					}
					bool flag12 = flag7;
					if (flag12)
					{
						this.assetPathHashSet.Add(text4);
					}
				}
				string str = "Target Path Assets : ";
				string str2 = this.assetPathHashSet.Count.ToString();
				string str3 = str + str2;
				ScanLog.Instance.LogInfo(str3);
				Singleton<PerformanceMeasurer>.Instance.StopStateExecutionMeasure(0L);
				bool flag13 = this.globalConfig.globalAssetsScanMode == GlobalConfig.e_Global_Assets_Scan_Mode.TargetOnlyWithDepends;
				if (flag13)
				{
					Singleton<PerformanceMeasurer>.Instance.StartStateExecutionMeasure("AssetDatabase.GetDependencies");
					string[] array2 = null;
					try
					{
						array2 = AssetDatabase.GetDependencies(Enumerable.ToArray<string>(this.assetPathHashSet));
					}
					catch (Exception ex)
					{
						ScanLog.UnityLogError("资源依赖加载失败，异常原因：" + ex.ToString() + "  " + ex.Message);
						array2 = new string[0];
					}
					bool flag14 = array2.Length != 0;
					if (flag14)
					{
						Singleton<PerformanceMeasurer>.Instance.StopStateExecutionMeasure(0L);
						Singleton<PerformanceMeasurer>.Instance.StartStateExecutionMeasure("UnionWith");
						this.assetPathHashSet.UnionWith(array2);
						Singleton<PerformanceMeasurer>.Instance.StopStateExecutionMeasure(0L);
						string str4 = "Target Depend Path Assets : ";
						string str5 = array2.Length.ToString();
						string str6 = str4 + str5;
						ScanLog.Instance.LogInfo(str6);
					}
				}
			}
			this.isCollectingOver = true;
			ToolsHelper.ShowProgressBar("Manager", "Collecting path info...", 1f, false);
		}

		// Token: 0x06000C9C RID: 3228 RVA: 0x00037990 File Offset: 0x00035B90
		private static bool IsPathContainedInArray(string[] _param0, string _param1)
		{
			for (int i = 0; i < _param0.Length; i++)
			{
				bool flag = _param0[i].Contains(_param1);
				if (flag)
				{
					return true;
				}
			}
			return false;
		}

		// Token: 0x06000C9D RID: 3229 RVA: 0x000379C8 File Offset: 0x00035BC8
		private bool CheckFile(string filePath)
		{
			string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
			string directoryName = Path.GetDirectoryName(filePath);
			string text = Path.GetExtension(filePath).ToLower().Replace(".", "");
			bool flag = !text.Contains("meta") && !Enumerable.Contains<string>(this.zhxLVshQSh, text) && !Enumerable.Contains<string>(this.t1KLZ6jKSt, fileNameWithoutExtension) && !AssetPathManager.IsPathContainedInArray(this.NnTLINGV30, directoryName);
			bool flag2 = true;
			bool flag3 = flag && this.DMKLF6jLbm != null;
			if (flag3)
			{
				for (int i = 0; i < this.DMKLF6jLbm.Length; i++)
				{
					bool flag4 = fileNameWithoutExtension.StartsWith(this.DMKLF6jLbm[i], StringComparison.Ordinal);
					if (flag4)
					{
						flag2 = false;
					}
				}
			}
			return flag && flag2;
		}

		// Token: 0x06000C9E RID: 3230 RVA: 0x00037A98 File Offset: 0x00035C98
		public bool DirTest(string relativeAssetPath)
		{
			bool flag = !this.isCollectingOver;
			if (flag)
			{
				throw new Exception("AssetPathManager has not setted up");
			}
			bool flag2 = string.IsNullOrEmpty(relativeAssetPath);
			if (flag2)
			{
				throw new Exception("Dir Filter failed: path is null or empty");
			}
			return this.assetPathHashSet.Contains(relativeAssetPath);
		}

		// Token: 0x06000C9F RID: 3231 RVA: 0x00037AE8 File Offset: 0x00035CE8
		public void WriterDirMap()
		{
			StreamWriter streamWriter = Singleton<FileStreamFactory>.Instance.GetStreamWriter(ToolsHelper.GetScanOutPut() + "/dirmap.csv");
			foreach (KeyValuePair<string, int> keyValuePair in this.dicDir)
			{
				streamWriter.WriteLine(keyValuePair.Value.ToString() + "," + ToolsHelper.QuoteString(keyValuePair.Key));
			}
			streamWriter.Close();
		}

		// Token: 0x040007A9 RID: 1961
		public static readonly AssetPathManager Instance = new AssetPathManager();

		// Token: 0x040007AA RID: 1962
		private bool isInit = false;

		// Token: 0x040007AB RID: 1963
		private bool isCollectingOver = false;

		// Token: 0x040007AC RID: 1964
		private List<string> assetPathList;

		// Token: 0x040007AD RID: 1965
		private HashSet<string> assetPathHashSet;

		// Token: 0x040007AE RID: 1966
		public string[] artPathList = null;

		// Token: 0x040007AF RID: 1967
		public string[] prefabPathList = null;

		// Token: 0x040007B0 RID: 1968
		public HashSet<string> scriptAssetsList = null;

		// Token: 0x040007B1 RID: 1969
		private Dictionary<string, int> dicDir = new Dictionary<string, int>();

		// Token: 0x040007B2 RID: 1970
		private GlobalConfig globalConfig;

		// Token: 0x040007B3 RID: 1971
		private string[] DMKLF6jLbm;

		// Token: 0x040007B4 RID: 1972
		private string[] NnTLINGV30;

		// Token: 0x040007B5 RID: 1973
		private string[] t1KLZ6jKSt;

		// Token: 0x040007B6 RID: 1974
		private string[] zhxLVshQSh;
	}
}
